; ORIGINAL: 3dt2.1/examples/splngrid.scm
; $Id: splngrid.scm,v 1.5 1998/12/09 23:01:21 jmb Exp $
; -------------------------------------------------------------------------
;   File: SPLNGRID.SCM
;  Notes: Scheme procedures to take a splgrid data type and intialize its
;         values to data read from a disk file.  The file format is:
;
;         u_points v_points
;         u_start_flag u_end_flag v_start_flag v_end_flag
;             - 1 a list exists, 0 no list available
;         list of grid points:  v (rows) by u (cols)
;             - each item ( %-8.4f ):  x  y  z
;         list of start/end tangent unit vectors
; -------------------------------------------------------------------------

; set the view
;
(view:dl 0 0 500 400)
(view:set (position 100 -200 100) (position 0 0 0) (gvector 0 0 1))
(view:set-size 200 200)

; set system options
;
(option:set "spline_param_lines" #t)
(option:set "u_param_lines" 4)
(option:set "v_param_lines" 4)

; grid file read procedure
;
(define splgrid:read (lambda (grid filename)
    (begin
        ; open data file
        ;(define dataport (open-input-file-path filename part-load-path))
	(define dataport (open-input-file filename))

        (define u_points (read dataport))
        (define v_points (read dataport))
        (define u_start  (read dataport))
        (define u_end    (read dataport))
        (define v_start  (read dataport))
        (define v_end    (read dataport))

        ; points list
        (define x               0)
        (define y               0)
        (define z               0)
        (define point_list (list))

        (if (and (> u_points 0) (> v_points 0))
            (begin
                (do ((count 0 (+ count 1))) ((= count (* u_points v_points)))
                    (begin
                        (set! x (read dataport))
                        (set! y (read dataport))
                        (set! z (read dataport))
                        (set! point_list (append point_list (list (position x y z))))
                    )
                )
            (splgrid:set-point-list grid point_list v_points u_points)
            )
            #f
        )

        ; u start tangent vectors
        (define unitvec_list (list))

        (if (> u_start 0)
            (begin
                (do ((count 0 (+ count 1))) ((= count u_points))
                    (begin
                        (set! x (read dataport))
                        (set! y (read dataport))
                        (set! z (read dataport))
                        (set! unitvec_list (append unitvec_list (list (gvector x y z))))
                    )
                )
                (splgrid:set-u-tanvec-list grid unitvec_list #t)
            )
        )

        ; u end tangent vectors
        (set! unitvec_list (list))

        (if (> u_end 0)
            (begin
                (do ((count 0 (+ count 1))) ((= count u_points))
                    (begin
                        (set! x (read dataport))
                        (set! y (read dataport))
                        (set! z (read dataport))
                        (set! unitvec_list (append unitvec_list (list (gvector x y z))))
                    )
                )
                (splgrid:set-u-tanvec-list grid unitvec_list #f)
            )
        )

        ; v start tangent vectors
        (define unitvec_list (list))

        (if (> v_start 0)
            (begin
                (do ((count 0 (+ count 1))) ((= count v_points))
                    (begin
                        (set! x (read dataport))
                        (set! y (read dataport))
                        (set! z (read dataport))
                        (set! unitvec_list (append unitvec_list (list (gvector x y z))))
                    )
                )
                (splgrid:set-v-tanvec-list grid unitvec_list #t)
            )
        )

        ; v end tangent vectors
        (set! unitvec_list (list))

        (if (> v_end 0)
            (begin
                (do ((count 0 (+ count 1))) ((= count v_points))
                    (begin
                        (set! x (read dataport))
                        (set! y (read dataport))
                        (set! z (read dataport))
                        (set! unitvec_list (append unitvec_list (list (gvector x y z))))
                    )
                )
                (splgrid:set-v-tanvec-list grid unitvec_list #f)
            )
        )

        ; close data file
        (close-input-port dataport)

        ; return success!
        #t
    )
))

; create a spline surface
;
(define grid (splgrid))
(splgrid:read grid "grid01.dat")

; display a spline surface face that interpolates the grid points
;
(face:spline-grid grid #t)

; ----------------------------------
;  End of SURFGRID.SCM
; ----------------------------------
